package gw.com.android.app;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.gwtsz.chart.output.utils.ChartConfig;
import com.gwtsz.gts2.hx.R;

import java.util.Timer;
import java.util.TimerTask;

import gw.com.android.app.dispatch.AppDispatch;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.UserHelpActivity;
import gw.com.android.ui.WebPageActivity;
import gw.com.android.ui.WelcomeActivity;
import gw.com.android.ui.account.LoginActivity;
import gw.com.android.ui.account.ModifyPassActivity;
import gw.com.android.ui.account.OpenAccountActivity;
import gw.com.android.ui.bulletin.BulletinDetailActivity;
import gw.com.android.ui.bulletin.BulletinDetailActivity2;
import gw.com.android.ui.bulletin.NotifyCategoryActivity;
import gw.com.android.ui.chart.ChartSetActivity;
import gw.com.android.ui.chart.LandChartActivity;
import gw.com.android.ui.chart.PortChartActivity;
import gw.com.android.ui.chart.PropertyActivity;
import gw.com.android.ui.gesture_pwd.GesturePwdCheckActivity;
import gw.com.android.ui.gesture_pwd.GesturePwdResetActivity;
import gw.com.android.ui.gesture_pwd.GesturePwdSettingActivity;
import gw.com.android.ui.gesture_pwd.PasswordCheckActivity;
import gw.com.android.ui.gesture_pwd.consts.GesturePwdConst;
import gw.com.android.ui.news.NewsDetailActivity;
import gw.com.android.ui.onlive.DiscussAddActivity;
import gw.com.android.ui.onlive.analyst.AnalystActivity;
import gw.com.android.ui.quote2.addquote.QuoteAddSelfActivity;
import gw.com.android.ui.quote2.editquote.EditOptionalActivity;
import gw.com.android.ui.system.AppCollectActivity;
import gw.com.android.ui.system.AppDesActivity;
import gw.com.android.ui.system.AppSettingActivity;
import gw.com.android.ui.trade.OrderCloseActivity;
import gw.com.android.ui.trade.OrderModifyActivity;
import gw.com.android.ui.trade.OrderOpenDialog;
import gw.com.android.ui.trade.PendingOrderModifyActivity;
import gw.com.android.utils.CacheUtils;
import gw.com.android.utils.ENV;
import gw.com.android.utils.PackStatis;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.app.AppActivities;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;
import www.com.library.view.LoadingDialog;

/**
 * Created by reeta.zou on 2016/11/17.
 * 项目中activity之间的跳转事件
 */
public class ActivityManager {

    public static void showWelcomeActivity(Activity activity) {
        Intent intent = new Intent(activity, WelcomeActivity.class);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * 开启登陆界面
     *
     * @param activity
     */
    public static void showLoginActivity(Activity activity) {
        Logger.i("showActivity 开启登陆界面");
        Intent intent = new Intent();
        intent.setClass(activity, LoginActivity.class);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * 返回登录界面,默认关闭当前界面
     *
     * @param activity
     */
    public static void backLogin(final Activity activity) {
        Logger.d("", "backLogin 返回到登陆界面");
        LoadingDialog.showLoading(activity);
        if (GTConfig.instance().mLastAccountType != GTConfig.ACC_TYPE_GUEST) {
            AppTerminal.instance().onDisconnect(AppContances.SERVER_TYPE_TRADE);
        }
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        LoadingDialog.hideLoading();
                        Intent intent = new Intent(activity, LoginActivity.class);
                        activity.startActivity(intent);
                        activity.finish();
                    }
                });
            }
        }, AppContances.TIMER_TASK_TIME);
    }

    /**
     * 返回登录界面
     *
     * @param activity
     */
    public static void backLogin(final Activity activity, final boolean isFinish) {
        Logger.d("", "backLogin 返回到登陆界面");
        LoadingDialog.showLoading(activity);
        if (GTConfig.instance().mLastAccountType != GTConfig.ACC_TYPE_GUEST) {
            AppTerminal.instance().onDisconnect(AppContances.SERVER_TYPE_TRADE);
        }
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        LoadingDialog.hideLoading();
                        resetApp(activity, isFinish);
                    }
                });
            }
        }, 50);
    }

    /**
     * 重置应用的相关数据和信息
     *
     * @param activity
     */
    private static void resetApp(Activity activity, boolean isFinish) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
        if (isFinish) {
            AppActivities.getSingleton().popAllActivityExceptOne(LoginActivity.class.getSimpleName());
        }
    }

    /**
     * 根据条件，执行跳转到 手势密码界面
     */
    public static void dispatchGesture(String tag, Activity activity, int mode) throws RuntimeException {
        //优先级最高的判定
        Log.d(tag, activity == null ? "null" : activity.toString());
        if (null == activity) {
            Log.d(tag, "Error/exception : activity is null!");
            return;
        }
        if (!activity.getResources().getBoolean(R.bool.ifOpenGesturePwdFunction)) {
            Log.d(tag, "ifOpenGesturePwdFunction 为 false，手势功能已关闭");
            return;
        }
        if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_REAL) {//如果不是真实用户
            return;
        }
        showGestureActivity(activity, mode); //  否则，就判定为是第一次,强制进入手势设置界面
        CacheUtils.writeFile(CacheUtils.CACHE_FIRST_COMING_2_4, "I'm here at 2.4!");//来过一次之后，就不再是第一次来了；
    }

    public static void showPwdCheckActivity(Activity activity) {
        if (null != activity) {
            Intent intent = new Intent(activity, PasswordCheckActivity.class);
            activity.startActivity(intent);
        }
    }

    /**
     * 进入手势界面，根据模式进行选择
     *
     * @param activity
     */
    private static void showGestureActivity(Activity activity, int mode) {
        Intent intent;
        if (mode == GesturePwdConst.MODE_SETTING)//setting模式，进入手势密码设置界面
        {
            intent = new Intent(activity, GesturePwdSettingActivity.class);
            String flag = CacheUtils.readFile(CacheUtils.CACHE_FIRST_COMING_2_4);
            if (!TextUtils.isEmpty(flag)) {//如果flag不是空，就说明不是第一次，就不用强制进入手势设置界面
                return;
            }

        } else if (mode == GesturePwdConst.MODE_RESET) {//密码重设界面
            intent = new Intent(activity, GesturePwdResetActivity.class);

        } else// 其他，进入手势密码校验界面
            intent = new Intent(activity, GesturePwdCheckActivity.class);

        activity.startActivity(intent);
        if (activity instanceof AppSettingActivity)
            activity.finish();
    }

    /**
     * 开启主面板
     *
     * @param activity
     */
    public static void showMainTab(Activity activity, String tag, int where) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.putExtra("where", where);
        if (tag.equals(ConfigType.TAB_HOME_TAG)) {
            if (ConfigUtil.instance().homeFucType() != 0) {
                intent.putExtra("mCurTag", ConfigType.TAB_HOME_TAG);
            } else {
                intent.putExtra("mCurTag", ConfigType.TAB_QUOTE_TAG);
            }
        } else {
            intent.putExtra("mCurTag", tag);
        }
        activity.startActivity(intent);
    }

    /**
     * 开启主面板
     *
     * @param activity
     */
    public static void showMainTab(Activity activity, String tag, Bundle extra) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.putExtras(extra);
        if (tag.equals(ConfigType.TAB_HOME_TAG)) {
            if (ConfigUtil.instance().homeFucType() != 0) {
                intent.putExtra("mCurTag", ConfigType.TAB_HOME_TAG);
            } else {
                intent.putExtra("mCurTag", ConfigType.TAB_QUOTE_TAG);
            }
        } else {
            intent.putExtra("mCurTag", tag);
        }
        activity.startActivity(intent);
    }

    /**
     * 开启开户界面
     */
    public static void showOpenAccount(Activity activity, String fromType) {
        showOpenAccount(activity, fromType, null);
    }

    public static void showOpenAccount(Activity activity, String fromType, String title) {
        Intent intent = new Intent(activity, OpenAccountActivity.class);
        intent.putExtra("fromType", fromType);
        intent.putExtra("title", title);
        activity.startActivity(intent);
    }

    /**
     * 开启帮助界面
     */
    public static void showHelpActivity(Activity activity, boolean hasBtn, String mBackTitle, String mTitle) {
        Intent intent = new Intent();
        intent.setClass(activity, UserHelpActivity.class);
        intent.putExtra("hasBtn", hasBtn);
        intent.putExtra("mBackTitle", mBackTitle);
        intent.putExtra("mTitle", mTitle);
        activity.startActivity(intent);
        if (hasBtn) {
            activity.finish();
        }
    }

    /**
     * 打开晒单界面
     *
     * @param activity
     */
    public static void showDiscussAdd(Activity activity) {
        Intent intent = new Intent(activity, DiscussAddActivity.class);
        activity.startActivityForResult(intent, AppContances.REQUEST_SYSTEM_CODE);
    }

    /**
     * 打开分析师主页
     *
     * @param userNo 分析師no
     */
    public static void showAnalystPage(String userNo, String accountNo, Activity activity) {
        Intent intent = new Intent(activity, AnalystActivity.class);
        intent.putExtra("userNo", userNo); //hxbj
        intent.putExtra("accountNo", accountNo); //60051330
        activity.startActivityForResult(intent, AppContances.REQUEST_SYSTEM_CODE);
    }

    /**
     * 打开系统嵌入网页界面
     */
    public static void showWebPageActivity(Activity activity, String title, String url, boolean enableShare) {
        DataItemDetail mItem = new DataItemDetail();
        if (enableShare) {
            mItem.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, ConfigType.ADS_DETAIL_TAG);
        }
        mItem.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, title);
        mItem.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, url);
        showWebPageActivity(activity, mItem, "");
    }

    /**
     * 打开系统嵌入网页界面
     *
     * @param leftTxt 显示title bar 最左边的文字
     */
    public static void showWebPageActivity(Activity activity, DataItemDetail mItem, String leftTxt) {
        if (mItem != null) {
            String urlString = mItem.getString(ConfigType.CONFIG_TYPE_TYPE_URL_TAG);
            if (AppDispatch.dispatch(activity, urlString)) {
                return;
            }
        }
        Intent intent = new Intent(activity, WebPageActivity.class);
        intent.putExtra("mItem", mItem);
        activity.startActivity(intent);
    }

    /**
     * 打开我的收藏视图
     *
     * @param activity
     */
    public static void showCollectActivity(Activity activity, String mBack, String mTitle) {
        Intent intent = new Intent(activity, AppCollectActivity.class);
        intent.putExtra("mBackTitle", mBack);
        intent.putExtra("mTitle", mTitle);
        activity.startActivity(intent);
    }

    /**
     * 打开产品介绍的视图
     *
     * @param activity
     */
    public static void showDesActivity(Activity activity, String mBack, String mTitle) {
        Intent intent = new Intent(activity, AppDesActivity.class);
        intent.putExtra("mBackTitle", mBack);
        intent.putExtra("mTitle", mTitle);
        activity.startActivity(intent);
    }

    /**
     * 打开隐私条款的视图
     *
     * @param activity
     */
    public static void showPrivacyActivity(Activity activity, String mBack, String mTitle) {

        //改成-打开H5页面
        DataItemDetail detail = new DataItemDetail();
        detail.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, "https://app.ftww.com/ft/privacy.html");
        detail.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, mTitle);
        ActivityManager.showWebPageActivity(activity, detail, null);

//        Intent intent = new Intent(activity, AppPrivacyActivity.class);
//        intent.putExtra("mBackTitle", mBack);
//        intent.putExtra("mTitle", mTitle);
//        activity.startActivity(intent);
    }

    /**
     * 打开系统设置视图
     *
     * @param activity
     */
    public static void showSettingActivity(Activity activity, String mTag, String mBackTitle, String mTitle) {
        Intent intent = new Intent(activity, AppSettingActivity.class);
        intent.putExtra("mTag", mTag);
        intent.putExtra("mBackTitle", mBackTitle);
        intent.putExtra("mTitle", mTitle);
        activity.startActivityForResult(intent, AppContances.REQUEST_SYSTEM_CODE);
    }

    /**
     * 打开修改密码视图
     *
     * @param activity
     */
    public static void showModifyActivity(Activity activity, String mBack) {
        Intent intent = new Intent(activity, ModifyPassActivity.class);
        intent.putExtra("mBack", mBack);
        activity.startActivity(intent);
    }

    /**
     * 打开消息中心视图
     *
     * @param activity
     */
    public static void showBulletinActivity(Activity activity, String mTitle) {
//        activity.startActivity(new Intent(activity, NotifyCentreActivity.class));// 不要进入消息类型列表了
        Intent intent = new Intent(activity, NotifyCategoryActivity.class);
        intent.putExtra("typeId", ENV.isUat() ? 153 : 68);//UAT下，typeId是153，prd下则是68
        intent.putExtra("typeName", "消息中心");
        activity.startActivity(intent);
    }

    /**
     * 打开资讯界面视图
     *
     * @param activity
     */
    public static void showNewsActivity(Activity activity) {
        /*Intent intent = new Intent(activity, NewsListActivity.class);
        activity.startActivity(intent);*/
    }

    /**
     * 打开消息详情视图
     *
     * @param activity
     */
    public static void showBulletinDetailActivity2(Activity activity, String url, String title, int mDataId, String type) {
        Intent intent = new Intent(activity, BulletinDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        intent.putExtra("mDataId", mDataId);
        intent.putExtra("type", type);
        intent.putExtra("fromType", AppContances.TYPE_FORM_BULLETIN);
        activity.startActivity(intent);
    }

    /**
     * 打开消息详情视图,通知栏
     *
     * @param activity
     */
    public static void showBulletinDetailActivity(Activity activity, String title, int mDataId, String type, String language, String url) {
        PackStatis.getToService(PackStatis.EventAction.CLICKPUSH.getValue(), PackStatis.EventCategory.MAIN.getValue(), url, mDataId + "");
        Intent intent = new Intent(activity, BulletinDetailActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("mDataId", mDataId);
        intent.putExtra("type", type);
        intent.putExtra("language", language);
        intent.putExtra("fromType", AppContances.TYPE_FORM_NOTIFY);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    /**
     * 打开消息详情视图
     *
     * @param activity
     */
    public static void showBulletinDetailActivityOpenApi2(Activity activity, String url, String title, String mDataId, String type) {
        Intent intent = new Intent(activity, BulletinDetailActivity2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        intent.putExtra("mDataId", mDataId);
        intent.putExtra("type", type);
        intent.putExtra("fromType", AppContances.TYPE_FORM_BULLETIN);
        activity.startActivity(intent);
    }

    /**
     * 打开消息详情视图,通知栏
     *
     * @param activity
     */
    public static void showBulletinDetailActivityOpenApi(Activity activity, String title, String mDataId, String type, String language, String url, String extend) {
        PackStatis.getToService(PackStatis.EventAction.CLICKPUSH.getValue(), PackStatis.EventCategory.MAIN.getValue(), url, mDataId + "");
        Intent intent = new Intent(activity, BulletinDetailActivity2.class);
        intent.putExtra("title", title);
        intent.putExtra("mDataId", mDataId);
        intent.putExtra("type", type);
        intent.putExtra("extend", extend);
        intent.putExtra("language", language);
        intent.putExtra("fromType", AppContances.TYPE_FORM_NOTIFY);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    /**
     * 打开资讯详情视图
     *
     * @param activity
     */
    public static void showNewsDetailActivity(Activity activity, String title, DataItemDetail item) {
        Intent intent = new Intent(activity, NewsDetailActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("item", item);
        activity.startActivityForResult(intent, AppContances.REQUEST_SYSTEM_CODE);
    }

    /**
     * 打开平仓界面
     *
     * @param act
     */
    public static void showCloseOrderActivity(Activity act, DataItemDetail mPositionInfo, int fromType) {
        Intent intent = new Intent(act, OrderCloseActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        Bundle bundle = new Bundle();
        bundle.putInt("uiCode", mPositionInfo.getInt(GTSConst.JSON_KEY_CODEID));
        bundle.putInt("id", mPositionInfo.getInt(GTSConst.JSON_KEY_ID));
        bundle.putInt("fromType", fromType);
        intent.putExtra("params", bundle);
        act.startActivityForResult(intent, AppContances.REQUEST_TRADE_CODE);
    }

    /**
     * 打开下单界面
     *
     * @param orderDir 买卖方向
     */
    public static void showOrderActivity(Activity act, DataItemDetail mTickModel, int orderDir, int fromType) {
        /*Intent intent = new Intent(act, OrderCreateActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("fromType", fromType);
        bundle.putInt("uiCode", mTickModel.getInt(GTSConst.JSON_KEY_CODEID));
        bundle.putInt("zoneType", mTickModel.getInt(GTSConst.JSON_KEY_ZONE));
        bundle.putInt("orderDir", orderDir);
        intent.putExtra("params", bundle);
        act.startActivityForResult(intent, AppContances.REQUEST_TRADE_CODE);*/
        int uiCode = mTickModel.getInt(GTSConst.JSON_KEY_CODEID);
        showOrderDialog(act, orderDir, uiCode);
    }

    public static void showOrderDialog(Activity act, int orderDir, int uiCode, int... uiCodesRange) {
        new OrderOpenDialog(act, orderDir, uiCode, uiCodesRange).show();
    }

    /**
     * 打开 止损止盈设置界面
     *
     * @param act
     * @param mPositionInfo
     */
    public static void showOrderModifyActivity(Activity act, DataItemDetail mPositionInfo, int fromType) {
        Intent intent = new Intent(act, OrderModifyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        Bundle bundle = new Bundle();
        bundle.putInt("uiCode", mPositionInfo.getInt(GTSConst.JSON_KEY_CODEID));
        bundle.putInt("id", mPositionInfo.getInt(GTSConst.JSON_KEY_ID));
        bundle.putInt("fromType", fromType);
        intent.putExtra("params", bundle);
        act.startActivityForResult(intent, AppContances.REQUEST_TRADE_CODE);
    }

    /**
     * 打开改单界面
     *
     * @param act
     */
    public static void showPendingOrderModifyActivity(Activity act, DataItemDetail mPositionInfo) {
        Intent intent = new Intent(act, PendingOrderModifyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        Bundle bundle = new Bundle();
        bundle.putInt("uiCode", mPositionInfo.getInt(GTSConst.JSON_KEY_CODEID));
        bundle.putInt("id", mPositionInfo.getInt(GTSConst.JSON_KEY_ID));
        intent.putExtra("params", bundle);
        act.startActivityForResult(intent, AppContances.REQUEST_TRADE_CODE);
    }

    /**
     * 打开图表界面视图:纵向
     */
    public static void showChartActivity(Activity activity, int mCodeid, int zoneType, int fromType) {
        ChartConfig.instance().mScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
        Intent intent = new Intent(activity, PortChartActivity.class);
        intent.putExtra("proCode", mCodeid);
        intent.putExtra("zoneType", zoneType);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        activity.startActivityForResult(intent, AppContances.REQUEST_CHART_CODE);
    }


    /**
     * 打开图表界面视图：横向
     */
    public static void showLandChartActivity(Activity activity, int mCodeid, int zoneType, int fromType) {
        ChartConfig.instance().mScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
        Intent intent = new Intent(activity, LandChartActivity.class);
        intent.putExtra("proCode", mCodeid);
        intent.putExtra("zoneType", zoneType);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        activity.startActivityForResult(intent, AppContances.REQUEST_CHART_CODE);
    }

    /**
     * 打开图表指标参数设置视图。
     *
     * @param act
     */
    public static void showChartSetActivity(Activity act, String mCurFormularName) {
        Intent intent = new Intent(act, ChartSetActivity.class);
        intent.putExtra("mCurFormularName", mCurFormularName);
        act.startActivityForResult(intent, AppContances.REQUEST_CHART_CODE);
    }

    /**
     * 打开合约属性视图。
     *
     * @param act
     */
    public static void showPropertyActivity(Activity act, int uiCode, int zone, String name, String subName) {
        Intent intent = new Intent(act, PropertyActivity.class);
        intent.putExtra("uiCode", uiCode);
        intent.putExtra("zone", zone);
        intent.putExtra("name", name);
        intent.putExtra("subName", subName);
        act.startActivityForResult(intent, AppContances.REQUEST_CHART_CODE);
    }

    /**
     * 打开编辑商品界面视图
     *
     * @param activity
     */
    public static void showEditOptionalActivity(Activity activity, String backTitle) {
        Intent intent = new Intent(activity, EditOptionalActivity.class);
        intent.putExtra("backTitle", backTitle);
        activity.startActivity(intent);
    }

    /**
     * 打开添加商品界面视图
     *
     * @param activity
     */
    public static void showQuoteSelfAddActivity(Activity activity) {
        Intent intent = new Intent(activity, QuoteAddSelfActivity.class);
        activity.startActivityForResult(intent, AppContances.REQUEST_SYSTEM_CODE);
    }
}
